<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <link rel="shortcut icon" href="https://www.baidu.com/favicon.ico" type="image/x-icon">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
</head>
<body>
<div style="display: flex;justify-content: center;align-items: center;margin-top: 10%">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input readonly="readonly" th:value="${session.admin.adminName}" type="text" name="name" required
                       lay-verify="required" placeholder="用户名"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">原密码</label>
            <div class="layui-input-inline">
                <input type="password" name="oldPass" required lay-verify="required" placeholder="请输入原密码"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="newPass" required lay-verify="required" placeholder="请输入新密码"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-inline">
                <input type="password" name="newPass1" required lay-verify="required" placeholder="再输一次"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                <button class="layui-btn" lay-submit lay-filter="formDemo">修改</button>
            </div>
        </div>
    </form>
</div>
<script th:src="@{/layui/layui.js}"></script>
<script th:inline="javascript">
    //Demo
    layui.use('form', function () {
        var form = layui.form;
        var $ = layui.$;
        //提交
        form.on('submit(formDemo)', function (data) {
            var name = $("input[name='name']").val();
            var pass = $("input[name='oldPass']").val();
            var pass1 = $("input[name='newPass']").val();
            var pass2 = $("input[name='newPass1']").val();
            if (pass1 != pass2) {
                layer.msg('两次密码不一致！', {
                    icon: 2,
                    time: 1500
                });
            } else if (pass1 == pass) {
                layer.msg('新旧密码不能一致！', {
                    icon: 2,
                    time: 1500
                });
            } else if(pass!=[[${session.admin.adminPassword}]]){
                console.log([[${session.admin.adminPassword}]])
                layer.msg('旧密码错误！', {
                    icon: 2,
                    time: 1500
                });
            } else {
                $.ajax({
                    url: "http://localhost:8080/admin/upPassword",
                    data: {
                        adminName: name,
                        adminPassword: pass1
                    },
                    success: function (res) {
                        if (res) {
                            layer.msg('修改成功', {
                                icon: 1,
                                time: 1500
                            });

                        } else {
                            layer.msg('修改失败', {
                                icon: 2,
                                time: 1500
                            });
                        }
                    }
                })
            }
            return false;
        });
    });
</script>
</body>
</html>